package client.comm.baoding.ui

import android.content.Intent
import android.util.Log
import android.view.View
import androidx.lifecycle.Observer
import androidx.lifecycle.lifecycleScope
import client.comm.baoding.BaseMiddleActivity
import client.comm.baoding.R
import client.comm.baoding.adapter.*
import client.comm.baoding.api.bean.UserInfo
import client.comm.baoding.databinding.ActivityFejBinding
import client.comm.baoding.ui.vm.FejViewModel
import client.comm.commlib.base.PagerFooterAdapter
import client.comm.commlib.dialog.ValidatePwdDialog
import client.comm.commlib.network.data.PagingJsonResult
import client.comm.commlib.toast.toast
import client.comm.commlib.utils.StoreUtil
import client.comm.commlib.widget.EmptyLayout

class FejActivity : BaseMiddleActivity<FejViewModel, ActivityFejBinding>() {

    override fun getLayout(): Int {
        return R.layout.activity_fej
    }

    val adapter: FejlPagingAdapter by lazy {
        FejlPagingAdapter(this, mImgLoader).apply {

            refreshLoading = {
//                if (itemCount == 0) {
//                    binding.emptyLayout.errorType = EmptyLayout.NETWORK_LOADING
//                }
            }
            refreshNotLoading = {
                binding.refreshLayout.isRefreshing = false
//                if (itemCount == 0) {
//                    binding.emptyLayout.errorType = EmptyLayout.NODATA
//                } else {
//                }
                binding.emptyLayout.visibility = View.GONE

            }
            refreshError = {
                if (itemCount == 0)
                    binding.emptyLayout.errorType = EmptyLayout.NETWORK_ERROR
                toast((it).error.message)
            }

        }
    }

    val headerAdapter: FejPagingHeaderAdapter by lazy {
        FejPagingHeaderAdapter(this).apply {

            hzCli = {
                if (it.isNullOrEmpty()) {
                    toast("请输入划转金额")
                } else {
                    val num = it
                    ValidatePwdDialog(this@FejActivity).apply {
                        title = "需要验证支付密码"
                        ok = {
                            viewModel.gmClick(it,num)
                        }
                    }.show()
                }
            }

            onChange = {
                viewModel.currLabel.value = it
                adapter.refresh()
            }
        }
    }

    override fun initView() {
        binding?.apply {
            event = this@FejActivity
            vm = viewModel
        }
        binding.emptyLayout.setOnClickListener {
            adapter.refresh()
        }
        binding.refreshLayout.setOnRefreshListener {
            viewModel.userInfo()
            adapter.refresh()
        }
        binding.recyclerView.adapter = adapter.withLoadStateHeaderAndFooter(
            footer = PagerFooterAdapter(),
            header = headerAdapter
        )

//
    }




    override fun onResume() {
        super.onResume()

        viewModel.userInfo()
        lifecycleScope.launchWhenResumed {

            viewModel.pagingDataAuto {
                val ret = viewModel.server.zcjl(it, viewModel.currLabel.value!!.toInt())
                PagingJsonResult(
                    list = ret.body()?.data?.record_list,
                    code = ret.body()?.code,
                    message = ret.body()?.message,
                    errorStr = ret.errorBody()?.string()
                )
            }.collect {
                adapter.submitData(it)
            }

        }
    }

    override fun observerUI() {
        viewModel.gmMyRet.observe(this, Observer {
            it?.let {
                if (it.isSuccess) {
                    toast("操作成功")
                    headerAdapter.guadanJe.value = ""
                    adapter.refresh()
                    viewModel.userInfo()
                } else {
                    toast(it.message)
                }
                viewModel.gmMyRet.value = null
            }
        })

        viewModel.callUser.observe(this, Observer {
            headerAdapter.notifyDataSetChanged()
        })
    }

    override fun initViewModel(): FejViewModel = createViewModel()


}